package com.itheima.service;

import cn.hutool.core.util.ObjectUtil;
import com.itheima.api.SmsCouponApi;
import com.itheima.api.SmsCouponProductCategoryRelationApi;
import com.itheima.api.SmsCouponProductRelationApi;
import com.itheima.dto.CreateCouponDto;
import com.itheima.pojo.SmsCoupon;
import com.itheima.pojo.SmsCouponProductCategoryRelation;
import com.itheima.pojo.SmsCouponProductRelation;
import com.itheima.vo.PageResult;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.expression.spel.ast.ValueRef;
import org.springframework.stereotype.Service;

/**
 * @author Hakuna Matata
 * @date 2022-10-11-20:04
 */
@Slf4j
@Service
public class CouponService {

    @DubboReference
    private SmsCouponApi couponApi;
    @DubboReference
    private SmsCouponProductCategoryRelationApi couponProductCategoryRelationApi;
    @DubboReference
    private SmsCouponProductRelationApi couponProductRelationApi;

    // 新增优惠券
    public void createCoupon(CreateCouponDto createCouponDto) {
        //往数据库里保存应有的数据
        //构建SmsCoupon对象
        SmsCoupon coupon = new SmsCoupon();
        //取出后者createCouponDto里的数据往coupon里set
        coupon = setCouponEntity(coupon, createCouponDto);
        //构建SmsCouponProductCategoryRelation对象
        SmsCouponProductCategoryRelation productCategoryRelationList = createCouponDto.getProductCategoryRelationList();
        //构建SmsCouponProductRelation对象
        SmsCouponProductRelation productRelationList = createCouponDto.getProductRelationList();
        //保存coupon
        Long couponId = couponApi.saveCoupon(coupon);

        if (ObjectUtil.isNotNull(productCategoryRelationList)) {
            //处理一下couponId
            productCategoryRelationList.setCouponId(couponId);
            System.out.println(productCategoryRelationList.getCouponId());
            couponProductCategoryRelationApi.saveCouponProductCategoryRelation(productCategoryRelationList);
            log.info(productCategoryRelationList.toString());
        }
        if (ObjectUtil.isNotNull(productRelationList)) {
            //处理一下couponId
            productRelationList.setCouponId(couponId);
            System.out.println(productRelationList.getCouponId());
            couponProductRelationApi.saveCouponProductRelation(productRelationList);
        }


    }

    //取出后者createCouponDto里的数据往coupon里set
    private SmsCoupon setCouponEntity(SmsCoupon coupon, CreateCouponDto createCouponDto) {
        coupon.setAmount(createCouponDto.getAmount());
        coupon.setCode(createCouponDto.getCode());
        coupon.setCount(createCouponDto.getCount());
        coupon.setEnableTime(createCouponDto.getEnableTime());
        coupon.setEndTime(createCouponDto.getEndTime());
        coupon.setId(createCouponDto.getId());
        coupon.setMemberLevel(createCouponDto.getMemberLevel());
        coupon.setMinPoint(createCouponDto.getMinPoint());
        coupon.setName(createCouponDto.getName());
        coupon.setNote(createCouponDto.getNote());
        coupon.setPerLimit(createCouponDto.getPerLimit());
        coupon.setPlatform(createCouponDto.getPlatform());
        coupon.setPublishCount(createCouponDto.getPublishCount());
        coupon.setReceiveCount(createCouponDto.getReceiveCount());
        coupon.setStartTime(createCouponDto.getStartTime());
        coupon.setType(createCouponDto.getType());
        coupon.setUseCount(createCouponDto.getUseCount());
        coupon.setUseType(createCouponDto.getUseType());
        return coupon;
    }

    //取出后者coupon里的数据往createCouponDto里set
    private CreateCouponDto setCreateCouponDto(CreateCouponDto createCouponDto, SmsCoupon coupon) {
        createCouponDto.setId(coupon.getId());
        createCouponDto.setAmount(coupon.getAmount());
        createCouponDto.setCode(coupon.getCode());
        createCouponDto.setCount(coupon.getCount());
        createCouponDto.setEnableTime(coupon.getEnableTime());
        createCouponDto.setEndTime(coupon.getEndTime());
        createCouponDto.setId(coupon.getId());
        createCouponDto.setMemberLevel(coupon.getMemberLevel());
        createCouponDto.setMinPoint(coupon.getMinPoint());
        createCouponDto.setName(coupon.getName());
        createCouponDto.setNote(coupon.getNote());
        createCouponDto.setPerLimit(coupon.getPerLimit());
        createCouponDto.setPlatform(coupon.getPlatform());
        createCouponDto.setPublishCount(coupon.getPublishCount());
        createCouponDto.setReceiveCount(coupon.getReceiveCount());
        createCouponDto.setStartTime(coupon.getStartTime());
        createCouponDto.setType(coupon.getType());
        createCouponDto.setUseCount(coupon.getUseCount());
        createCouponDto.setUseType(coupon.getUseType());
        return createCouponDto;
    }

    //删除优惠券
    public void deleteCoupon(Integer id) {
        //删除coupon
        couponApi.deleteById(id);
        //根据couponId删除couponProductCategoryRelation
        couponProductCategoryRelationApi.deleteByCouponId(id);
        //根据couponId删除couponProductRelation
        couponProductRelationApi.deleteByCouponId(id);
    }

    /**
     * 根据优惠券名称和类型分页获取优惠券列表
     */
    public PageResult getCouponListByCouponNameAndByPage(String name, Integer pageNum, Integer pageSize, Integer type) {
        //name可能为空
        //type可能为空
        return couponApi.getCouponListByCouponNameAndByPage(name, pageNum, pageSize, type);
    }

    //更新优惠券
    public void updateCoupon(Integer id, CreateCouponDto createCouponDto) {
        //找到coupon
        SmsCoupon coupon = couponApi.findCouponById(id);
        //更新SmsCoupon对象
        //取出后者createCouponDto里的数据往coupon里set
        coupon = setCouponEntity(coupon, createCouponDto);
        couponApi.updateCoupon(coupon);
        if (ObjectUtil.isNotNull(createCouponDto.getProductCategoryRelationList())) {
            //更新SmsCouponProductCategoryRelation对象
            couponProductCategoryRelationApi.updateCouponProductCategoryRelation(createCouponDto.getProductCategoryRelationList());
        }
        if (ObjectUtil.isNotNull(createCouponDto.getProductRelationList())) {
            //更新SmsCouponProductRelation对象
            couponProductRelationApi.updateCouponProductRelation(createCouponDto.getProductRelationList());
        }


    }

    //获取单个优惠券的详细信息
    public CreateCouponDto findCouponById(Integer id) {
        //构建CreateCouponDto
        CreateCouponDto createCouponDto = new CreateCouponDto();
        //查找coupon信息
        SmsCoupon coupon = couponApi.findCouponById(id);
        //查找SmsCouponProductCategoryRelation
        SmsCouponProductCategoryRelation couponProductCategoryRelation = couponProductCategoryRelationApi.findByCouponId(id);
        //查找SmsCouponProductRelation
        SmsCouponProductRelation couponProductRelation = couponProductRelationApi.findByCouponId(id);

        createCouponDto = setCreateCouponDto(createCouponDto, coupon);
        createCouponDto.setProductCategoryRelationList(couponProductCategoryRelation);
        createCouponDto.setProductRelationList(couponProductRelation);

        return createCouponDto;
    }
}
